package leetcode01_10;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author shhjiang_001
 * @create 2020-03-08 22:04
 */
public class Solution01 {
    public int[] towSum1(int[] nums, int target){
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0;i<nums.length;i++){
            int complement = target - nums[i];
            if(map.containsKey(complement)){
                return new int[] {map.get(complement),i};
            }
            map.put(nums[i],i);
        }
        return null;
    }
    public int[] towSum2(int[] nums, int target){
        for (int i = 0; i < nums.length-1; i++) {
            for (int j = i+1; j <nums.length; j++) {
                if (nums[i] + nums[j] == target){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }
    public int[] towSum(int[] nums, int target){
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target-nums[i])){
                return new int[]{map.get(target-nums[i]),i};
            }else {
                map.put(nums[i],i);
            }
        }
        return null;
    }
    @Test
    public void test(){
        Solution01 solution01 = new Solution01();
        int[] ints = solution01.towSum(new int[]{2, 7, 11, 15}, 9);
        System.out.println(Arrays.toString(ints));
    }
}
